package cn.liberals.basic.controller;

import cn.liberals.basic.exception.BusinessException;
import cn.liberals.basic.service.ILoginService;
import cn.liberals.basic.utils.AjaxResult;
import cn.liberals.basic.vo.LoginVo;
import cn.liberals.basic.vo.RegisterVo;
import cn.liberals.basic.vo.WechatVo;
import cn.liberals.user.domain.Logininfo;
import cn.liberals.user.service.impl.LogininfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /* 
    * @Title: account
    * @Description: 账号密码登录
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/9 19:17  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginVo loginVo) {
        try {
            // 调用业务层的实现登录，返回值是token+logininfo用户对象信息
            Map<String, Object> map = loginService.account(loginVo);
            // 返回成功的信息，并将token+logininfo放到前端methess响应
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /* 
    * @Title: mobileVerificationCode
    * @Description: 手机验证码登录
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/9 19:18  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/mobileVerificationCode")
    public AjaxResult mobileVerificationCode(@RequestBody LoginVo loginVo){
        try {
            Map<String, Object> map = loginService.loginMobileVerificationCode(loginVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /*
    * @Title: wechat
    * @Description: 创建微信登录接口
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/10 17:28
    * @Parameters:
    * @Return
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WechatVo wechatVo){
        try {
            // 调用业务层进行登录
            return loginService.wechat(wechatVo);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /* 
    * @Title: binder
    * @Description: 微信用户账号绑定
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/10 20:03  
    * @Parameters: 
    * @Return 
    */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WechatVo wechatVo){
        try {
            // 调用业务层进行登录,返回值就是token+logininfo用户对象信息
            Map<String, Object> map = loginService.binder(wechatVo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
